'use strict'

const Controller = require('egg').Controller

class HomeController extends Controller {
  //获取数据
  async TickList() {
    const { ctx } = this
    const list = await this.app.mysql.select('ticket')
    ctx.body = {
      code: 200,
      msg: '车票数据获取成功',
      list,
    }
  }

  //添加车票
  async Tickadd() {
    const { ctx } = this
    const { trains, start, over, gotime, didtime, falg } = ctx.request.body
    const res = await this.app.mysql.insert('ticket', {
      trains,
      start,
      over,
      gotime,
      didtime,
      falg: '已提交',
    })
    if (res) {
      const list = await this.app.mysql.select('ticket')
      ctx.body = {
        code: 200,
        msg: '车票数据添加成功',
        list,
      }
    } else {
      ctx.body = {
        code: 400,
        msg: '车票数据添加失败',
      }
    }
  }

  //删除
  async Tickdel() {
    const { ctx } = this
    const { id } = ctx.query
    const res = await this.app.mysql.delete('ticket', {
      id,
    })
    if (res.affectedRows === 1) {
      const list = await this.app.mysql.select('ticket')
      ctx.body = {
        code: 200,
        msg: '删除成功',
        list,
      }
    } else {
      ctx.body = {
        code: 404,
        msg: '删除失败',
      }
    }
  }
}

module.exports = HomeController
